package com.ruoyi.web.sso.util;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

public class HttpUtil {

    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "/" + param;
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            int cot = 0;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    public static String sendPost(String url, String param) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = null;            // 打开和URL之间的连接
            conn = (HttpURLConnection) realUrl.openConnection();            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");    // POST方法
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.connect();            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");            // 发送请求参数
            out.write(param);            // flush输出流的缓冲
            out.flush();            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


    public static String sendPut(String url, String param) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = null;            // 打开和URL之间的连接
            conn = (HttpURLConnection) realUrl.openConnection();            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("PUT");    // POST方法
            // 设置通用的请求属性
            conn.setRequestProperty("Authorization", "");

            conn.connect();            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");            // 发送请求参数
            out.write(param);            // flush输出流的缓冲
            out.flush();            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static List<Date> getDateList(String startYear, String startMonth) {
        Map totalMap = loop(startYear, startMonth);
        List<Date> list =  DateUtil.getDates(Integer.parseInt(startYear), Integer.parseInt(startMonth));
        if (totalMap.size() > 0) {
            for (Object key : totalMap.keySet()) {
                String key1 = (String) key;
                String type = (String) totalMap.get(key);
                if (StringUtils.equals(type, "2") && StringUtils.equals( StringUtil.getSubStr(key1, 1, "-", "-"), startMonth)) {
                    list.add( DateUtil.stringByYMD(key1));
                }
                for (int i = 0; i < list.size(); i++) {
                    if (StringUtils.equals(key1,  DateUtil.dateByYMD1(list.get(i)))) {
                        if (StringUtils.equals(type, "1")) {
                            list.remove(i);
                            i--;
                        }
                    }
                }
            }
        }
        return list;
    }


    public static Map loop(String year, String month) {
        String s = HttpUtil.sendGet("http://opendata.baidu.com/api.php?query=" + year + "%E5%B9%B4" + month + "%E6%9C%88&resource_id=6018&format=json", "");

        Map totalMap = new TreeMap();
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);

        List list = (List) map.get("data");
        if (list == null || list.size() == 0) {
            return totalMap;
        }

        Map<String, Object> innerMap = (Map<String, Object>) (list.get(0));
        if (innerMap.get("holiday") == null) {
            return totalMap;
        }

        List holidayList = new ArrayList();
        Object obj = innerMap.get("holiday");
        if (obj instanceof List) {
            holidayList = (List) obj;
        } else {
            holidayList.add((Map) obj);
        }

        for (int i = 0; i < holidayList.size(); i++) {
            List innerHolidayList = (List) ((Map) holidayList.get(i)).get("list");
            for (int j = 0; j < innerHolidayList.size(); j++) {
                Map el = (Map) innerHolidayList.get(j);
                totalMap.put(el.get("date"), el.get("status"));
            }
        }
        return totalMap;
    }


    public static List<Date> getDateList(String startYear, String startMonth, List<Map<String, Object>> dateList) {
        List<Date> list =  DateUtil.getDates(Integer.parseInt(startYear), Integer.parseInt(startMonth));
        if (dateList.size() > 0) {
            for (Map<String, Object> map : dateList) {
                Date key = (Date) map.get("startAt");
                String key1 =  DateUtil.dateByYMD(key);
                String type = (String) map.get("type");
                if (StringUtils.equals(type, "2") && StringUtils.equals( StringUtil.getSubStr(key1, 1, "-", "-"), startMonth)) {
                    list.add( DateUtil.stringByYMD(key1));
                }
                for (int i = 0; i < list.size(); i++) {
                    if (StringUtils.equals(key1,  DateUtil.dateByYMD1(list.get(i)))) {
                        if (StringUtils.equals(type, "1")) {
                            list.remove(i);
                            i--;
                        }
                    }
                }
            }
        }
        return list;
    }


    public static void main(String[] args) {
        String year = "274ab37721274115b9da1810c2284eab";
        String month = "044bcaa8566edfd6ed59b3518830d354";

        String s = HttpUtil.sendPut("https://face-1256472418.cos.ap-nanjing.myqcloud.com/1.jpg", "");
        System.out.println(s);

    }
}
